package com.ly.demo.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author liuyang
 * @Date 2024/6/17 下午4:42
 **/
public class CookieUtil {
    public static Cookie getCookie(String name) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return getCookie(request, name);
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        return getCookie(name, cookies);
    }

    public static Cookie getCookie(String name, Cookie[] cookies) {
        if (StringUtils.isBlank(name)) return null;
        if (null == cookies || cookies.length == 0) return null;
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName()) && cookie.getMaxAge() != 0) return cookie;
        }
        return null;
    }

    public static String getCookieValue(String name) {
        Cookie cookie = getCookie(name);
        if (null == cookie) return null;
        return cookie.getValue();
    }

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie cookie = getCookie(request, name);
        if (null == cookie) return null;
        return cookie.getValue();
    }

    public static String getCookieValue(String name, Cookie[] cookies) {
        Cookie cookie = getCookie(name, cookies);
        if (null == cookie) return null;
        return cookie.getValue();
    }

    public static void addCookie(
            HttpServletRequest request,
            HttpServletResponse response,
            String name,
            String value,
            int maxAge) {
        addCookie(request, response, name, value, null, maxAge, true);
    }

    public static void addCookie(
            HttpServletRequest request,
            HttpServletResponse response,
            String name,
            String value,
            String domain,
            int maxAge, boolean httpOnly) {
        String contextPath = request.getContextPath();
        addCookie(request, response, name, value, domain, contextPath, maxAge, httpOnly);
    }

    public static void addCookie(HttpServletRequest request,
                                 HttpServletResponse response,
                                 String name,
                                 String value,
                                 String domain,
                                 String contextPath,
                                 int maxAge,
                                 boolean httpOnly) {
        Cookie cookie = getCookie(request, name);
        if (null == cookie)
            cookie = new Cookie(name, value);
        if (maxAge > -1)
            cookie.setMaxAge(maxAge);
        cookie.setSecure(request.isSecure());
        cookie.setValue(value);
        if (contextPath == null || contextPath.isEmpty()) {
            cookie.setPath("/");
        } else {
            cookie.setPath(contextPath);
        }
        if (domain != null && !domain.isEmpty()) {
            cookie.setDomain(domain);
        }
        if (httpOnly) {
            cookie.setHttpOnly(true);
        }
        response.addCookie(cookie);
    }

    public static void expireCookie(HttpServletResponse response,
                                    String name,
                                    String domain,
                                    String contextPath,
                                    int maxAge,
                                    boolean httpOnly) {
        Cookie cookie = getCookie(name);
        if (null == cookie) return;
        cookie.setHttpOnly(httpOnly);
        cookie.setMaxAge(maxAge);
        cookie.setDomain(domain);
        cookie.setPath(contextPath);
        expireCookie(response, cookie);
    }

    public static void expireCookie(HttpServletResponse response,
                                    Cookie cookie) {
        if (null == cookie)
            return;
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    public static void expireCookie(HttpServletRequest request,
                                    HttpServletResponse response,
                                    String name) {
        Cookie cookie = getCookie(request, name);
        expireCookie(response, cookie);
    }
}
